package com.wwwc.index.web.servlet;

import java.net.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
public class CheckMail {

    public static void main(String s[]) {
	if (s.length != 3) {
	    System.out.println("===============================================================");
	    System.out.println("[1][List mail providers]");
	    System.out.println("[2][CheckMail]{[mailServer:192.168.1.104] [user] [password]}");
	    System.out.println("===============================================================");

	    java.util.Properties properties = System.getProperties();
	    Session session = Session.getInstance(properties, null);
	    Provider[] providers = session.getProviders();
	    for (int i =0; i<providers.length; i++) {
		System.out.println(providers[i]);
	    }
	    System.exit(0);
	}

	CheckMail t = new CheckMail();
	java.util.Properties properties = System.getProperties();
	try {
	    Session session = Session.getInstance(properties, null);
	    Store store = session.getStore("pop3");
	    store.connect(s[0], s[1], s[2]);
	    Folder folder = store.getFolder("INBOX");
	    folder.open(Folder.READ_ONLY);
	    Message[] messages = folder.getMessages();
	    for(int i=0; i<messages.length; i++) {
		Flags flags = messages[i].getFlags();
		String[] sf = flags.getUserFlags();
		/*
		for (int j = 0; j<sf.length; j++) {
		    System.out.println("Flag"+sf[j].toString());
		}
		*/
		System.out.println("From:"+messages[i].getFrom()[0]);
		System.out.println("Subject:"+messages[i].getSubject());
		
		BufferedReader in  = new BufferedReader(new InputStreamReader(messages[i].getInputStream()));
		String temp;
		while((temp=in.readLine()) != null) {
		    System.out.println(temp);
		}
	    }
	    folder.close(false);
	    store.close();
	}
	catch (Exception e){
	    e.printStackTrace();
	}
	
	try { 
	    CheckMail t1 = new CheckMail();
	    int i = t1.checkMyMail(s[0], s[1], s[2]);
	    if (i==0) {
		System.out.println("No mail waiting.");
	    }
	    else {
		System.out.println
		    ("There " + (i==1?"is " :"are ") + i +
		     " message" +(i==1?"":"s")+ " waiting.");
	    }
	}
	catch (Exception e) {
	    e.printStackTrace();
	}
	
    }

    private void send(BufferedWriter out, String s) throws IOException {
	out.write(s+"\n");
	out.flush();
    }

    private String receive(BufferedReader in) throws IOException {
	String in_string;
	in_string=in.readLine();
	if(in_string.startsWith("-ERR")) {
	    System.out.println("ERROR!");
	    System.exit(1);
	    return in_string;
	}
	System.out.println(in_string);
	return in_string;
    }

    private int checkMyMail
	(String server, String user, String pass) throws IOException {
	Socket s = new Socket(server, 110);
	BufferedReader in  = new BufferedReader(new InputStreamReader(s.getInputStream()));
	BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

	receive(in);
	send(out, "USER " + user);
	receive(in);
	send(out, "PASS " + pass);
	receive(in);
	
	return GetNumberOfMessages(in, out);
    }

    public int GetNumberOfMessages
	(BufferedReader in, BufferedWriter out) throws IOException {
	int i = 0;
	String s;

	send(out, "LIST");
	receive(in);

	while((s = receive(in)) != null) {
	    if (!(s.equals("."))) {
		i++;
	    }
	    else {
		return i;
	    }
	}
	out.close();
	in.close();
	return 0;
    }

}



